Number.prototype.toFixed=function(len) 
{ 
	 if(isNaN(len)||len==null) 
	 { 
	  len = 0; 
	 } 
	 else 
	 { 
		  if(len<0) 
		  { 
		   len = 0; 
		  } 
	 } 

    return Math.round(this * Math.pow(10,len)) / Math.pow(10,len); 

}

function unformatCurrency(srcString){
    var str = srcString;
    
    while (str.indexOf(",") != -1){
    	str = str.replace(",", "");
    }
    
    return str;
}

function formatCurrency(srcNumber) {
	var txtNumber = '' + srcNumber;
	
	if (isNaN(txtNumber) || txtNumber == "") {
		return '';
	}else {
		var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
		var arrNumber = txtNumber.split('.');
		arrNumber[0] += '.';
		
		do {
			arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
		} while (rxSplit.test(arrNumber[0]));
		
		if (arrNumber.length > 1) {
			return arrNumber.join('');
		}else {
			return arrNumber[0].split('.')[0];
		}
	}
}

function formatPrecision(srcNumber, precision){
	var txtNumber = '' + srcNumber;
	
	if (isNaN(txtNumber) || txtNumber == "") return '';
	
	txtNumber = '' + parseFloat(txtNumber).toFixed(precision);

	return formatCurrency(txtNumber);

}

function addZeros(decimal, precision)
{
	if (precision) {
	     if (decimal.toString().length == 0)
	          decimal = 0;
	     
	     var zeros='';
	     
	     numberOfZeros = (precision - decimal.toString().length);
	     
	     for (z=0 ; numberOfZeros > z ; z++)
	          zeros+='0';
	          
	     return decimal + zeros;
	}
	
	return '';
}

function nullToZero(strValue) {
	if (strValue==null || strValue=="") strValue = 0;
	
	return unformatCurrency(strValue);
}

function getValidNumeric(obj) {
	if(obj != null) {
		if(obj.value == null || obj.value == '') {
			return 0;
		} else {
			return nullToZero(obj.value);
		}
	} else {
		return 0;
	}
}

function trim(inputStr) {
	var result = inputStr;
	
	while (result.substring(0,1) == " ") {
		result = result.substring(1,result.length);
	}

	while (result.substring(result.length-1, result.length) == " ") {
		result = result.substring(0, result.length-1)
	}
	
	return result
}

/**
 * validatation functions
 */
 
function validateNumber(Object){

	if ( Object.value == "" ){
		return true;
	}
	
	if (!IsDouble( Object.value )){
		Object.style.backgroundColor = "#FFFF00";
		return false;
	}else{
		var str = Object.value;
		while (str.charAt(0) == "0"){
			if (str.charAt(1) == "." || str.length == 1){
				break;
			}			
			str = str.substring(1, str.length);
		}

		Object.value = str;

		Object.style.backgroundColor = "#F7F8F9";

		return true;	
	}	
}





function validateInteger(Object){

	if ( Object.value == "" ){
		return true;
	}
	
	if (!IsInteger( Object.value )){
		Object.style.backgroundColor = "#FFFF00";
		return false;
	}else{
		Object.style.backgroundColor = "#F7F8F9";
		return true;	
	}	
}

function validateDate(Object){

	if ( Object.value == "" ){
		return true;
	}
	
	if (!IsDate( Object.value )){
		Object.style.backgroundColor = "#FFFF00";
		return false;
	}else{
		Object.style.backgroundColor = "#F7F8F9";
		return true;	
	}	
	
}

function IsDate(s) {
    var na=s.split("-");
    var e=true;
    
    if (na.length!=3) {
    	e=false;
    } else {      		 
       	if (IsYear(na[0]) && IsMonth(na[1]) && IsDay(na[2])) {
         	var d=na[2]
         	var m=na[1];
          	var y=na[0];
          	
          	if ((e) && (y<1000))
          		e=false;
          		
          	if (e) {
            	v=new Date(y, m-1, d);
                if (v.getMonth()!=m-1)
                	e=false;
            }
        } else {
	    	e=false;
     	}     
    }
    
    return e;
}

function AddMonth(str, a) {
    var na=str.split("-");

 	var d=na[2]
 	var m=na[1];
  	var y=na[0];
          	
	v=new Date(y, m-1, d);
    v.setMonth(v.getMonth() + a);
    
	var s = '';
    s += v.getYear() + "-";
    
	if (v.getMonth() < 9)
		s += '0' + (v.getMonth()+1) + "-";
	else
		s += (v.getMonth()+1) + "-";

	if (v.getDate() < 10)
	    s += '0' + v.getDate();
	else
	    s += v.getDate();
    
    return s;
     	
}   
 
function AddDay(str, a) {
    var na=str.split("-");

 	var d=na[2]
 	var m=na[1];
  	var y=na[0];
          	
	v=new Date(y, m-1, d);
    v.setDate(v.getDate() + a);
    
	var s = '';
    s += v.getYear() + "-";
    
	if (v.getMonth() < 9)
		s += '0' + (v.getMonth()+1) + "-";
	else
		s += (v.getMonth()+1) + "-";

	if (v.getDate() < 10)
	    s += '0' + v.getDate();
	else
	    s += v.getDate();
    
    return s;
     	
}   

function IsYear( str )
{
	if ( str.length!=4)
   		return false;

	for ( var i = 0 ; i < str.length; i ++ )
	{
		if ( str.charAt(i) < "0" || str.charAt(i) > "9" )
			return false;
	}
	
	if ((str<1900) || (str>3000)) return false;
	return true;
      		  	
}    

function IsMonth( str )
{
	if (( str.length==0) ||(str.length>2))
       		return false;

	for ( var i = 0 ; i < str.length; i ++ )
	{
		if ( str.charAt(i) < "0" || str.charAt(i) > "9" )
			return false;
	}
	
	if ((str<1) || (str>12)) return false;
	return true;
      		  	
}

function IsDay( str )
{
	if (( str.length==0) ||(str.length>2))
       		return false;

	for ( var i = 0 ; i < str.length; i ++ )
	{
		if ( str.charAt(i) < "0" || str.charAt(i) > "9" )
			return false;
	}
	
	if ((str<1) || (str>31)) return false;
	return true;
}

function IsDouble( str1 )
{	  var str = str1 + '';
	if ( str.length == 0 )
		return false;
	var docnum = 0;
	for ( var i = 0 ; i < str.length; i ++ )
	{
		if ( str.charAt(i) == "." ){
			docnum ++;
			continue;
		}else{
			if ( str.charAt(i) < "0" || str.charAt(i) > "9" )
			return false;
		}
	}
	if (docnum > 1){
		return false;
	}
	return true;
}
function IsInteger( str )
{	
	if ( str.length == 0 )
		return false;	
		
	for ( var i = 0 ; i < str.length; i ++ )
	{		
		if ( str.charAt(i) < "0" || str.charAt(i) > "9" )
			return false;
	}
	return true;
}
	
function IsAlpha( str )
{	
	if ( str.length == 0 )
		return false;
		
	str = str.toUpperCase();
	
	for ( var i = 0 ; i < str.length; i ++ )
	{
		if ( str.charAt(i) < "A" || str.charAt(i) > "Z" )
			return false;
	}
	return true;
}
	
function IsAlnum( str )
{	
	if ( str.length == 0 )
		return false;
		
	str = str.toUpperCase();
		
	for ( var i = 0 ; i < str.length; i ++ )
	{
		if ( !( ( (str.charAt(i) >= "A") && (str.charAt(i) <= "Z") ) || 
			( (str.charAt(i) >= "0") && (str.charAt(i) <= "9") ) ) )
			return false;
	}
	return true;
}
	
function IsTel( str )
{	
	if ( str.length == 0 )
		return false;
		
	for ( var i = 0 ; i < str.length; i ++ )
	{
		if ( ( str.charAt(i) < "0" || str.charAt(i) > "9" ) && str.charAt(i) != "-" )
			return false;
	}
	return true;
}
	
function IsEMail( str )
{	
	var pos;
		
	if ( ( pos = str.indexOf( "@" ) ) == -1 )
	{	
		return false;
	}
	else if ( ( pos = str.indexOf("@", pos + 1) )  != -1 )
	{
		return false;
	}
			
	return true;
}

/**********************************************************************/
/*Function name        :isDate(s,f)                          */
/*Usage of this function  :To check s is a valid format            */
/*Input parameter required:s=input string                     */
/*                  f=input string format                */
/*                   =1,in mm/dd/yyyy format                */
/*                   else in dd/mm/yyyy                     */
/*Return value            :if is a valid date return 1                */
/*                  else return 0                     */
/*Function required       :isPositiveInteger()                         */
/**********************************************************************/   

function isDate(s, f) {
	var a1=s.split("/");
    var a2=s.split("-");
    var e=true;
    
    if ((a1.length!=3) && (a2.length!=3)) {
    	e=false;
    } else {
    	if (a1.length==3)
      		var na=a1;
      		
       	if (a2.length==3)
      		var na=a2;
      		  
       	if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) {
       		if (f==1) {
       			var d=na[1],m=na[0];
           	} else {
           		var d=na[0],m=na[1];
            }
              
          	var y=na[2];
          	
          	if ((e) && (y<1000))
          		e=false;
          		
          	if (e) {
            	v=new Date(m+"/"+d+"/"+y);
            	
                if (v.getMonth()!=m-1)
                	e=false;
            }
        } else {
	    	e=false;
     	}     
    }
    
    return e;
    
}
   
 

